﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.Paint.DisplayCoverage.CApPaintDisplayCoverageManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using EngineeringInternalExtension.DataTypes.Paint;
using EngineeringInternalExtension.DevelopmentPrototype.DataTypes;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.Paint.DisplayCoverage;

public class CApPaintDisplayCoverageManager
{
  private CApPaintDisplayCoverageViewModel m_apVM;
  private ITx2Or3DimensionalGeometry m_paintBrush;
  private TxFrame m_paintBrushTipFrame;
  private List<TxColor> m_emphasizeColorList = new List<TxColor>();
  private TxColor m_emphasizeColorForGrid = new TxColor(TxColor.TxColorGray);
  private TxObjectList m_emphasizeMeshes = new TxObjectList();
  private bool m_HSRFilter;
  private bool m_displayCoverageMode = true;
  private bool m_displayBrush;
  private bool m_displayThicknessValue;
  private double m_consequentTimeInterval = 0.015;
  private double m_turnAngle = 80.0;
  private bool m_loadRegistrySettings = true;
  private StreamWriter loggerWriter;
  private static CApPaintDisplayCoverageManager instance;

  private CApPaintDisplayCoverageManager() => this.m_emphasizeColorForGrid.Transparency = 1.0;

  public static CApPaintDisplayCoverageManager Instance
  {
    get
    {
      if (CApPaintDisplayCoverageManager.instance == null)
        CApPaintDisplayCoverageManager.instance = new CApPaintDisplayCoverageManager();
      return CApPaintDisplayCoverageManager.instance;
    }
  }

  public bool LoadRegistrySettings
  {
    get => this.m_loadRegistrySettings;
    set => this.m_loadRegistrySettings = value;
  }

  public double ConsequentTimeInterval
  {
    get => this.m_consequentTimeInterval;
    set => this.m_consequentTimeInterval = value;
  }

  public double TurnAngle
  {
    get => this.m_turnAngle;
    set => this.m_turnAngle = value;
  }

  public TxObjectList EmphasizeMeshes
  {
    get => this.m_emphasizeMeshes;
    set => this.m_emphasizeMeshes = value;
  }

  public List<TxColor> EmphasizeColorList
  {
    get => this.m_emphasizeColorList;
    set
    {
      this.m_emphasizeColorList = value;
      TxApplicationEx.PaintManager().SetCoverageColorMap(this.m_emphasizeColorList);
      TxApplicationEx.PaintManager().ApplyColors();
      TxApplication.RefreshDisplay();
    }
  }

  public ITx2Or3DimensionalGeometry PaintBrush
  {
    get => this.m_paintBrush;
    set => this.m_paintBrush = value;
  }

  public TxFrame PaintBrushTipFrame
  {
    get => this.m_paintBrushTipFrame;
    set => this.m_paintBrushTipFrame = value;
  }

  public bool HSRFilter
  {
    get => this.m_HSRFilter;
    set
    {
      this.m_HSRFilter = value;
      TxPaintSettings paintSettings = TxApplicationEx.PaintManager().PaintSettings;
      paintSettings.IgnoreHiddenSurfaces = this.m_HSRFilter;
      TxApplicationEx.PaintManager().PaintSettings = paintSettings;
      if (this.m_apVM == null)
        return;
      this.m_apVM.HSRFilter = this.m_HSRFilter;
    }
  }

  public bool DisplayCoverageMode
  {
    get => this.m_displayCoverageMode;
    set
    {
      this.m_displayCoverageMode = value;
      TxPaintSettings paintSettings = TxApplicationEx.PaintManager().PaintSettings;
      paintSettings.DisplayCoverageMode = this.m_displayCoverageMode;
      TxApplicationEx.PaintManager().PaintSettings = paintSettings;
      if (this.m_apVM != null)
        this.m_apVM.DisplayCoverageMode = this.m_displayCoverageMode;
      CUiPaintDisplayCoverageUtil.UpdateMeshVisibility();
    }
  }

  public bool DisplayBrush
  {
    get => this.m_displayBrush;
    set
    {
      this.m_displayBrush = value;
      if (this.m_apVM == null)
        return;
      this.m_apVM.DisplayBrush = this.m_displayBrush;
    }
  }

  public bool DisplayThicknessValue
  {
    get => this.m_displayThicknessValue;
    set
    {
      this.m_displayThicknessValue = value;
      TxPaintSettings paintSettings = TxApplicationEx.PaintManager().PaintSettings;
      paintSettings.DisplayThicknessValue = this.m_displayThicknessValue;
      TxApplicationEx.PaintManager().PaintSettings = paintSettings;
      if (this.m_apVM == null)
        return;
      this.m_apVM.DisplayThicknessValue = this.m_displayThicknessValue;
    }
  }

  public void IntializeViewModel(CApPaintDisplayCoverageViewModel apVM)
  {
    this.m_apVM = apVM;
    this.LoadPaintSettingsFromRegistry();
    this.EmphasizeColorList = CUiPaintDisplayCoverageUtil.ConvertToTxColor(this.m_apVM.SelectedColorList);
  }

  public void ReleaseViewModel()
  {
    string thicknessScaleName = this.m_apVM.SelectedThicknessScaleName;
    this.m_apVM = (CApPaintDisplayCoverageViewModel) null;
    this.BlankGrid();
    this.StoreColorListToRegistry();
    CUiPaintDisplayCoverageUtil.StoreThicknessScaleNameToRegistry(thicknessScaleName);
    this.StorePaintSettingsToRegistry();
  }

  public void ReleasePaintBrushDefinitions()
  {
    this.PaintBrush = (ITx2Or3DimensionalGeometry) null;
    this.PaintBrushTipFrame = (TxFrame) null;
  }

  public void LoadPaintSettingsForSimulatation()
  {
    if (this.m_loadRegistrySettings)
    {
      this.LoadOverlapValuesValuesFromRegistry();
      this.LoadPaintSettingsFromRegistry();
    }
    double num = this.ConsequentTimeInterval + TxApplication.ActiveDocument.SimulationPlayer.TimeInterval;
    TxPaintSettings paintSettings = TxApplicationEx.PaintManager().PaintSettings;
    paintSettings.OverlapConsequentTimeInterval = num;
    paintSettings.OverlapTurnAngle = this.TurnAngle;
    paintSettings.IgnoreHiddenSurfaces = this.HSRFilter;
    paintSettings.DisplayCoverageMode = this.DisplayCoverageMode;
    TxApplicationEx.PaintManager().PaintSettings = paintSettings;
  }

  public void StoreColorListToRegistry()
  {
    CUiPaintDisplayCoverageUtil.StoreColorListToRegistry(this.EmphasizeColorList);
  }

  public void LoadPaintSettingsFromRegistry()
  {
    this.DisplayCoverageMode = CUiPaintDisplayCoverageUtil.LoadDisplayCoverageModeFromRegistry();
    this.DisplayBrush = CUiPaintDisplayCoverageUtil.LoadDisplayBrushFromRegistry();
    this.DisplayThicknessValue = CUiPaintDisplayCoverageUtil.LoadDisplayThicknessValueFromRegistry();
    this.HSRFilter = CUiPaintDisplayCoverageUtil.LoadIgnoreHiddenSurfacesFromRegistry();
  }

  public void StorePaintSettingsToRegistry()
  {
    CUiPaintDisplayCoverageUtil.StoreDisplayCoverageModeToRegistry(this.DisplayCoverageMode);
    CUiPaintDisplayCoverageUtil.StoreDisplayBrushToRegistry(this.DisplayBrush);
    CUiPaintDisplayCoverageUtil.StoreDisplayThicknessValueToRegistry(this.DisplayThicknessValue);
    CUiPaintDisplayCoverageUtil.StoreIgnoreHiddenSurfacesToRegistry(this.HSRFilter);
  }

  private void LoadOverlapValuesValuesFromRegistry()
  {
    double consequentTimeInterval;
    double turnAngle;
    CUiPaintDisplayCoverageUtil.LoadOverlapValuesFromRegistry(out consequentTimeInterval, out turnAngle);
    this.ConsequentTimeInterval = consequentTimeInterval;
    this.TurnAngle = turnAngle;
  }

  public void Clear() => TxApplicationEx.PaintManager().Clear();

  public void UnEmphasizeMeshes()
  {
    foreach (ITxObject emphasizeMesh in (Collection<ITxObject>) this.EmphasizeMeshes)
    {
      if (emphasizeMesh is ITx2Or3DimensionalGeometry)
        Tx2Or3DimensionalGeometryEx.StopFaceEmphasizeByWire(emphasizeMesh as ITx2Or3DimensionalGeometry, this.m_emphasizeColorForGrid, (TxColorEmphasizePriority) 3);
    }
    ((Collection<ITxObject>) this.EmphasizeMeshes).Clear();
  }

  private bool IsValidForPainting()
  {
    TxObjectList<Tx2Or3DimensionalGeometry> ofDisplayedParts = CUiPaintDisplayCoverageUtil.GetMeshEntitiesOfDisplayedParts();
    return (ofDisplayedParts != null && ((Collection<Tx2Or3DimensionalGeometry>) ofDisplayedParts).Count != 0) & (this.PaintBrush != null && TxFrame.op_Inequality(this.PaintBrushTipFrame, (ITxObject) null));
  }

  public void RemoveCoverageFromMesh()
  {
    TxApplicationEx.PaintManager().RemoveCoverage();
    this.BlankGrid();
  }

  public void DisplayGrid()
  {
    this.UnEmphasizeMeshes();
    CUiPaintDisplayCoverageUtil.DisplayAllMeshes();
    foreach (ITx2Or3DimensionalGeometry entitiesOfDisplayedPart in (Collection<Tx2Or3DimensionalGeometry>) CUiPaintDisplayCoverageUtil.GetMeshEntitiesOfDisplayedParts())
    {
      Tx2Or3DimensionalGeometryEx.EmphasizeFaceByWire(entitiesOfDisplayedPart, this.m_emphasizeColorForGrid, (TxColorEmphasizePriority) 3);
      ((Collection<ITxObject>) this.EmphasizeMeshes).Add((ITxObject) entitiesOfDisplayedPart);
    }
  }

  public void BlankGrid()
  {
    this.UnEmphasizeMeshes();
    CUiPaintDisplayCoverageUtil.UpdateMeshVisibility();
  }

  public void SetTesselationColors(
    ITxObject tessallation,
    List<uint> affectedPoints,
    TxColor color)
  {
    int length = affectedPoints.Count<uint>();
    TxVertexColorDataEx[] vertexColorDataExArray = new TxVertexColorDataEx[length];
    for (int index = 0; index < length; ++index)
      vertexColorDataExArray[index] = new TxVertexColorDataEx(affectedPoints[index], color);
    Tx2Or3DimensionalGeometryEx.SetTesselationColors(tessallation as ITx2Or3DimensionalGeometry, vertexColorDataExArray);
  }

  public void ExportToFile(ITxObject tessallation, List<uint> affectedPoints)
  {
    TxDocument activeDocument = TxApplication.ActiveDocument;
    int num = 0;
    foreach (uint affectedPoint in affectedPoints)
    {
      if (affectedPoint != 0U)
        ++num;
    }
    this.loggerWriter.WriteLine($"Time:{Math.Round(activeDocument.SimulationPlayer.CurrentTime, 4).ToString()} Entity: {tessallation.Id} from {((ITxObject) tessallation.Collection).Name}" + (affectedPoints.Count == 0 ? " No coverage" : $" TotalPoints:{affectedPoints.Count.ToString()} AffectedPoints:{num.ToString()}"));
    for (int index = 0; index < affectedPoints.Count; ++index)
    {
      if (affectedPoints[index] != 0U)
        this.loggerWriter.Write($"{index.ToString()}({affectedPoints[index].ToString()}) ");
    }
    this.loggerWriter.WriteLine();
    this.loggerWriter.WriteLine();
  }

  public void GetAffectedPointsInMesh()
  {
    try
    {
      foreach (Tx2Or3DimensionalGeometry entitiesOfDisplayedPart in (Collection<Tx2Or3DimensionalGeometry>) CUiPaintDisplayCoverageUtil.GetMeshEntitiesOfDisplayedParts())
      {
        List<uint> uintList = new List<uint>();
        List<uint> affectedPointsInMesh = TxApplicationEx.PaintManager().GetAffectedPointsInMesh((ITx2Or3DimensionalGeometry) entitiesOfDisplayedPart);
        if (this.loggerWriter != null)
          this.ExportToFile((ITxObject) entitiesOfDisplayedPart, affectedPointsInMesh);
      }
    }
    catch (Exception ex)
    {
    }
  }

  public void ExportToFile(ITxObject tessallation, List<float> affectedPoints)
  {
    TxDocument activeDocument = TxApplication.ActiveDocument;
    int num = 0;
    foreach (double affectedPoint in affectedPoints)
    {
      if (affectedPoint > 0.0)
        ++num;
    }
    this.loggerWriter.WriteLine($"Time:{Math.Round(activeDocument.SimulationPlayer.CurrentTime, 4).ToString()} Entity: {tessallation.Id} from {((ITxObject) tessallation.Collection).Name}" + (affectedPoints.Count == 0 ? " No coverage" : $" TotalPoints:{affectedPoints.Count.ToString()} AffectedPoints:{num.ToString()}"));
    for (int index = 0; index < affectedPoints.Count; ++index)
    {
      if ((double) affectedPoints[index] > 0.0)
        this.loggerWriter.Write($"{index.ToString()}({affectedPoints[index].ToString("F1")}) ");
    }
    this.loggerWriter.WriteLine();
    this.loggerWriter.WriteLine();
  }

  public void GetTotalThicknessInMesh()
  {
    try
    {
      foreach (Tx2Or3DimensionalGeometry entitiesOfDisplayedPart in (Collection<Tx2Or3DimensionalGeometry>) CUiPaintDisplayCoverageUtil.GetMeshEntitiesOfDisplayedParts())
      {
        List<float> floatList = new List<float>();
        List<float> totalThicknessInMesh = TxApplicationEx.PaintManager().GetTotalThicknessInMesh((ITx2Or3DimensionalGeometry) entitiesOfDisplayedPart);
        if (this.loggerWriter != null)
          this.ExportToFile((ITxObject) entitiesOfDisplayedPart, totalThicknessInMesh);
      }
    }
    catch (Exception ex)
    {
    }
  }

  public void CalculateStaticCoverage()
  {
    try
    {
      if (!this.IsValidForPainting())
        return;
      TxObjectList<Tx2Or3DimensionalGeometry> ofDisplayedParts = CUiPaintDisplayCoverageUtil.GetMeshEntitiesOfDisplayedParts();
      TxApplicationEx.PaintManager().SetMeshList(ofDisplayedParts);
      CUiPaintDisplayCoverageUtil.DisplayAllMeshes();
      TxApplicationEx.PaintManager().ApplyCoverage(this.PaintBrush, this.PaintBrushTipFrame, -1.0, false);
      CUiPaintDisplayCoverageUtil.UpdateMeshVisibility();
    }
    finally
    {
      TxApplication.ActiveSelection.Clear();
      TxApplication.RefreshDisplay();
    }
  }

  public void OpenAutoTestFile(string AutoTestPath)
  {
    if (AutoTestPath == null)
      return;
    if (File.Exists(AutoTestPath))
      File.Delete(AutoTestPath);
    this.loggerWriter = File.AppendText(AutoTestPath);
  }

  public void CloseAutoTestFile()
  {
    if (this.loggerWriter == null)
      return;
    this.loggerWriter.Close();
    this.loggerWriter = (StreamWriter) null;
  }
}
